package com.burger.counter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.Region;
import com.burger.counter.util.DBConnection;

public class RegionDao {
	public Region[] getRegionDetails(){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Region regionList[]=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.getRegion;
			prest = con.prepareStatement(prepStatement);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				regionList = new Region[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Region p = new Region();
					p.setRegionId(rs.getInt(1));
					p.setRegionName(rs.getString(2));
					regionList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return regionList;
	}

	
	public Region[] getRegionandTaxDetails(){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Region regionList[]=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.getRegionAndTax;
			prest = con.prepareStatement(prepStatement);
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				regionList = new Region[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Region p = new Region();
					p.setRegionName(rs.getString(1));
					p.setTax(rs.getDouble(2));
					regionList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return regionList;
	}
	
	public Region[] getTaxDetails(String regionId){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Region regionList[]=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.getTaxDetails;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, regionId);
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				regionList = new Region[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Region p = new Region();
					p.setRegionName(rs.getString(1));
					p.setTax(rs.getDouble(2));
					regionList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return regionList;
	}
	
	
}
